package com.kedll.library.addimg;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.kedll.library.R;
import com.kedll.library.adapter.MyArrayAdapter;
import com.kedll.library.contants.OnItemClickListener;
import com.kedll.library.util.ImageLoadUtil;
import com.kedll.library.util.Parse;

import java.util.Map;

public class AddImgAdapter01 extends MyArrayAdapter<Map<String, Object>> {

    /**
     * 数据相关
     */
    protected int width;// 控件宽

    protected Parse parse;
    /**
     * 接口
     */
    protected OnItemClickListener l;// 内部控件点击事件


    public AddImgAdapter01(Context context, int width) {
        super(context, 0);
        this.width = width;
        parse = Parse.getInstance();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder = null;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_add_img01, null);
            holder.fl_addimg = convertView.findViewById(R.id.fl_addimg);
            holder.iv_addimg = convertView.findViewById(R.id.iv_addimg);
            holder.view_click = convertView.findViewById(R.id.view_click);
            holder.tv_title = convertView.findViewById(R.id.tv_title);
            holder.tv_count = convertView.findViewById(R.id.tv_count);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        LayoutParams lp = holder.fl_addimg.getLayoutParams();
        lp.width = width;
        lp.height = width / 3 * 2;
        holder.fl_addimg.setLayoutParams(lp);
        LayoutParams lp1 = holder.iv_addimg.getLayoutParams();
        lp1.width = width;
        lp1.height = width / 3 * 2;
        holder.iv_addimg.setLayoutParams(lp1);

        Map<String, Object> map = getItem(position);
        String path = null;
        try {
            path = parse.isNull(
                    parse.parseList(map.get("imageList")).get(0)
                            .get("imagePath"));
        } catch (Exception e) {
            // TODO: handle exception
            path = "";
        }
        holder.tv_title.setText(parse.isNull(map.get("bucketName")));
        holder.tv_count.setText(parse.isNull(map.get("count")));
        String url = "file://" + path;
        ImageLoadUtil.getInstance().ImageLoad(getContext(), url, holder.iv_addimg);
//        imageLoader.displayImage("file://" + path, holder.iv_addimg, options,
//                animateFirstListener);
        holder.view_click.setOnClickListener(new MyOnClickListener(
                holder.view_click, map, position));
        return convertView;
    }

    /**
     * 设置接口
     *
     * @param l
     */
    public void setOnItemClickListener(OnItemClickListener l) {
        this.l = l;
    }

    static class Holder {
        FrameLayout fl_addimg;// 图片父布局
        ImageView iv_addimg;// 图片
        View view_click;// 点击遮罩
        TextView tv_title;// 相册名
        TextView tv_count;// 当前相册中的图片数量
    }

    /**
     * 重写点击事件
     */
    class MyOnClickListener implements OnClickListener {

        private View v;
        private Map<String, Object> item;
        private int position;

        public MyOnClickListener(View v, Map<String, Object> item,
                                 int position) {
            this.v = v;
            this.item = item;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (l != null) {
                l.itemClick(v, item, position);
            }
        }

    }

}
